﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BGSpaceShooter
{
    /// <summary>
    /// Klasa zarzadzajaca contentem. Wydaje odpowiednie modele, tekstury
    /// </summary>
    public class GameContentManager
    {
        private ContentManager contentManager;

        /// <summary>
        /// Mapuje enumy na nazwy contentu, troche lipa przy ewentualnie drugim mysliwcu
        /// </summary>
        private Dictionary<BGSpaceShooterServer.GameObjectName, string> ContentNames;


        #region SINGLETON
        private static GameContentManager instance;

        private GameContentManager(ContentManager contentManager)
        {
            this.contentManager = contentManager;
            ContentNames = new Dictionary<BGSpaceShooterServer.GameObjectName, string>();
            ContentNames.Add(BGSpaceShooterServer.GameObjectName.Viper, "viper2");
            ContentNames.Add(BGSpaceShooterServer.GameObjectName.Asteroid, "asteroid");
            ContentNames.Add(BGSpaceShooterServer.GameObjectName.Bullet, "bullet"); //not a mistake
        }

        public static GameContentManager Content
        {
            get
            {
                if (instance == null)
                {
                    throw new Exception("Game content manager not initialized!");
                }

                return instance;
            }
        }

        public static void Initialize(ContentManager contentManager)
        {
            instance = new GameContentManager(contentManager);
        }

        #endregion
        /// <summary>
        /// Zwraca odpowiedni model dla danego typu obiektu
        /// </summary>
        /// <param name="gameObjectType"></param>
        /// <returns></returns>
        public Model GetModel(BGSpaceShooterServer.GameObjectName gameObjectType)
        {
            if (ContentNames.ContainsKey(gameObjectType))
            {
                Model model = contentManager.Load<Model>("Models/" + ContentNames[gameObjectType]);
                if (model == null)
                    throw new Exception("Missing model for " + gameObjectType);
                return model;
            }
            throw new Exception("Unknown model name for " + gameObjectType);
        }

        public Model GetModel(string name)
        {
            Model model = contentManager.Load<Model>("Models/" + name);
            if (model == null)
                throw new Exception("Missing model for " + name);
            return model;

        }

        public SpriteFont GetFont()
        {
            return contentManager.Load<SpriteFont>("Resources/UIFont");
        }

        public Texture2D GetTexture(string name)
        {
            return contentManager.Load<Texture2D>("Resources/" + name);
        }
    }
}
